package com.industrial.governance.controller;

import com.industrial.common.core.utils.uuid.IdUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.governance.domain.DgAutoExportTimeSet;
import com.industrial.governance.mapper.DgAutoExportTimeSetMapper;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/data/timeset")
public class DgAutoExportTimeSetController {

    @Autowired
    private DgAutoExportTimeSetMapper dgAutoExportTimeSetMapper;

    @PostMapping("/setTimePoint")
    public AjaxResult setTimePoint(@RequestBody DgAutoExportTimeSet dgAutoExportTimeSet) {
        if (dgAutoExportTimeSet == null || dgAutoExportTimeSet.getTimePoint() == null) {
            return AjaxResult.warn("时间不能为空");
        }
        dgAutoExportTimeSet.setBaseId(IdUtils.simpleUUID());
        dgAutoExportTimeSetMapper.insert(dgAutoExportTimeSet);
        return AjaxResult.success();
    }

    @GetMapping("/getTimePoint")
    public AjaxResult getTimePoint() {
        DgAutoExportTimeSet tem = dgAutoExportTimeSetMapper.selectTimePoint();
        if (tem == null) {
            tem = new DgAutoExportTimeSet();
        }
        return AjaxResult.success(tem);
    }

    @PutMapping("/updateTimePoint")
    public AjaxResult updateTimePoint(@RequestBody DgAutoExportTimeSet dgAutoExportTimeSet) {
        if (dgAutoExportTimeSet == null || dgAutoExportTimeSet.getTimePoint() == null) {
            return AjaxResult.warn("时间不能为空");
        }
        DgAutoExportTimeSet oldDb = dgAutoExportTimeSetMapper.selectTimePoint();
        if (oldDb == null) {
            dgAutoExportTimeSet.setBaseId(IdUtils.simpleUUID());
            dgAutoExportTimeSetMapper.insert(dgAutoExportTimeSet);
        } else {
            oldDb.setTimePoint(dgAutoExportTimeSet.getTimePoint());
            dgAutoExportTimeSetMapper.update(oldDb);
        }
        return AjaxResult.success();
    }
}
